<?php

use think\migration\Migrator;
use think\migration\db\Column;

class CreateLogMysqlDriver extends Migrator
{
    const TBL_NAME = 'log';
    const RELATED_TBL_NAME = 'user';

    public function change()
    {
        // create the table
        $table  =  $this->table(addTablePrefix(self::TBL_NAME),[
            'engine'=>'InnoDB',
            'comment' => '日志表',
            'signed' => false
        ]);
        $table
            ->addColumn('type', 'string',array('limit'=>32,'default'=>'','comment'=>'日志信息'))
            ->addColumn('route', 'string',array('limit'=>32,'default'=>'','comment'=>'访问路由'))
            ->addColumn('log',  'text',array('comment'=>'日志信息'))
            ->addColumn('data', 'text',array('comment'=>'请求数据'))
            ->addColumn('user_id', 'integer',array('signed' => false,'null'=>true,'comment'=>'用户'))
            ->addColumn('create_time', 'integer',array('signed' => false, 'default'=>0,'comment'=>'创建时间'))
            ->addIndex(array('create_time'))
            ->addForeignKey('user_id', addTablePrefix(self::RELATED_TBL_NAME), 'id', ['delete'=> 'SET_NULL', 'update'=> 'NO_ACTION'])
            ->create();
    }
}
